unit uLocalization;

interface

uses Windows, pxml;

type
  TLocaleFile = class(TPXMLDocument)
  private
    function GetLocaleID: LCID;
    function GetLocaleStr: string;
    function GetLocaleName: WideString;
  public
    property LocaleStr: string read GetLocaleStr;
    property LocaleName: WideString read GetLocaleName;
    property LocaleId: LCID read GetLocaleID;

    function GetLocaleString(AStringId: string; ShowPathForUnknown: boolean): string;
  end;

procedure AutoLoadLocale;
function LoadLocaleFile(AFile: string): boolean;
function GetString(AStringId: string; ShowPathForUnknown: boolean = true): string;

implementation

uses SysUtils, uConfig;

function AppDir: string;
begin
  Result := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
end;

var
  vLocal: TLocaleFile = nil;

function LoadLocaleFile(AFile: string): boolean;
begin
  Result := false;
  AFile := AppDir+'Data\'+AFile+'.lcxml';
  if FileExists(AFile) then begin
    if (vLocal<>nil) then FreeAndNil(vLocal);
    vLocal := TLocaleFile.Create;
    try
      vLocal.Load(AFile);
      Result := true;
    except
      FreeAndNil(vLocal);
    end;
  end;
end;

(*
  AStringID format:
  #<entryname>
  #<sectionname>\...\<entryname>
*)
function GetString(AStringId: string; ShowPathForUnknown: boolean): string;
begin
  Result := AStringId;
  if copy(AStringId,1,1) <> '#' then exit;
  Delete(AStringId,1,1);
  if (AStringId='') then exit;
  Result := 'unk:'+AStringId;

  if (vLocal=nil) then exit;
  Result := vLocal.GetLocaleString(AStringId, ShowPathForUnknown);
end;

{ TLocaleFile }

function TLocaleFile.GetLocaleID: LCID;
begin
  Result := 0;
  if (Self.DocumentRoot=nil) then exit;
  Result := StrToIntDef(Self.DocumentRoot.getAttribute('lcid'), 0);
end;

function TLocaleFile.GetLocaleName: WideString;
begin
  Result := '';
  if (Self.DocumentRoot=nil) then exit;
  Result := Self.DocumentRoot.getAttribute('name');
  if Result='' then Result := Self.GetLocaleStr;
end;

function TLocaleFile.GetLocaleStr: string;
begin
  Result := '';
  if (Self.DocumentRoot=nil) then exit;
  Result := Self.DocumentRoot.getAttribute('locale');
end;

function TLocaleFile.GetLocaleString(AStringId: string; ShowPathForUnknown: boolean): string;
var sec: TPXMLNode;
begin
  if (ShowPathForUnknown) then Result := AStringId
  else Result := '';
  if (DocumentRoot=nil) then exit;

  sec := FindElementPath(Self.DocumentRoot, AStringId);
  if (sec=nil) then exit;
  Result := sec.Value;
end;

function keyValue(AKey: string): string;
var i: integer;
begin
  Result := '';
  for i := 1 to ParamCount do
    if copy(ParamStr(i),1,Length(akey))=akey then begin
      Result := copy(ParamStr(i),Length(AKey)+1,255);
      break;
    end;
end;

procedure AutoLoadLocale;
var tmp: LCID;
    s: string;
begin
  s := Config.AsString['main.language',''];
  if (s='') then begin
    s := 'en-US';
    tmp := GetThreadLocale();
    if (tmp = 1049) then s := 'ru-RU';
  end;

  if not LoadLocaleFile(s) then
    LoadLocaleFile('en-US')
  else Config.AsString['main.language','en-US'] := s;

  Config.Commit;
end;


initialization
  //AutoLoadLocale;

finalization
  if (vLocal<>nil) then FreeAndNil(vLocal);
  
end.
